import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { IModalWindow } from '../model/interfaces'
import { store } from '../redux/store'
import { tmpActions } from './tmp.slice'


const initialState: IModalWindow = {
  title: '',
  isLoading: false,
  isError: false,
  isShow: false,
  buttonSet: [],
  errorMessage: '',
  content: '',
  footer: '',
  onOk: () => {},
  onClose: () => {}
}

export const modalSlice = createSlice({
  name: 'modal',
  initialState,
  reducers: {
    setModalData(state, action: PayloadAction<any>) {
      const {title, isShow, errorMessage, isError, isLoading, onClose, content, footer, buttonSet, onOk} = action.payload
      state.title = title || state.title
      state.isShow = isShow || state.isShow
      state.isError = isError || state.isError
      state.isLoading = isLoading || state.isLoading
      state.errorMessage = errorMessage || state.errorMessage
      state.content = content || state.content
      state.footer = footer || state.footer
      state.buttonSet = buttonSet || state.buttonSet
      state.onOk = onOk || state.onOk
      state.onClose = onClose || state.onClose
    },
    onShowModal(state, action: PayloadAction<boolean>) {
        state.isShow = action.payload
    },
    setFooter(state, action: PayloadAction<any>) {
      state.footer = action.payload
    },
    setButtonSet(state, action: PayloadAction<any>) {
      state.buttonSet = action.payload
    }
  }
})

export const modalActions = modalSlice.actions
export const modalReducer = modalSlice.reducer
